package hcs.model;

import java.util.Observable;

import stubs.IPhysicalBoiler;
import stubs.PhysicalFactory;

import hcs.interfaces.ClockObserver;
import hcs.interfaces.StateObservable;

/**
 * Represents the house boiler.
 */
public class Boiler extends StateObservable implements ClockObserver{
	private double currentTemperature;
	private double targetTemperature;
	private IPhysicalBoiler physBoiler;
	
	/**
	 * Create a new <code>Boiler</code> object.
	 */
	public Boiler() {
		physBoiler = PhysicalFactory.getPhysicalBoiler();
		currentTemperature = physBoiler.getCurrentTemperature();
		targetTemperature = physBoiler.getTargetTemperature();
		Clock.getInstance().addObserver(this);
	}
	
	/**
	 * Gets the current boiler temperature.
	 * @return the current boiler temperature.
	 */
	public double getCurrentTemperature() {
		return currentTemperature;
	}
	
	/**
	 * Sets the new boiler temperature, and notifies observers if the new requested temperature
	 * is different from the current one.
	 * @param temperature the new requested temperature.
	 */
	public void setTargetTemperature(double temperature) {
		if (temperature != this.targetTemperature) {
			this.targetTemperature = temperature;
			physBoiler.setTargetTemperature(targetTemperature);
			this.setChanged();
			this.notifyObservers();
		}
	}

	public void update(Observable o, Object arg) {
		double newCurrentTemp = physBoiler.getCurrentTemperature();
		if (newCurrentTemp != currentTemperature) {
			this.setChanged();
			currentTemperature = newCurrentTemp;
			this.notifyObservers();
		}
	}

	/**
	 * Gets the current target temperature.
	 * @return the current target temperature.
	 */
	public double getTargetTemperature() {
		return targetTemperature;
	}
}
